﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for KPIBreakDownSerializer
    /// </summary>
    public class KPIBreakDownSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                   "ID",
                   "No",
                   "Description",
                   "isActive",
                   "KPIParent",
                   "GroupKPI",
                   "CategoryKPI",
                   "TypeKPI",
                   "SubTypeKPI",
                   "Periode",
                   "Weight",
                   "UoMMax",
                   "UoMMin"
			       
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            int UserID = Convert.ToInt16(HttpContext.Current.Request.Params["User"]);
            int year = DateTime.Now.Year;// HttpContext.Current.Request.Params["Year"].Replace("undefined", string.Empty) == string.Empty ? DateTime.Now.Year : Convert.ToInt16(HttpContext.Current.Request.Params["Year"]);
            Employee User = new EmployeeFacade().GetEmployeeByID(Convert.ToInt16(HttpContext.Current.Request.Params["User"]));

            SetupYearFacade yf = new SetupYearFacade();
            SetupYear objYear = yf.GetByID(1);
            if (objYear != null)
                year = objYear.EndYear;

            IList<KPI> result = new List<KPI>();
            KPIFacade pf = new KPIFacade();
            int iTotalRecords = 0;
            int iTotalDisplayRecords = 0;
            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();
            sysLogFacade sysLogFacade = new sysLogFacade();
            sysLog objSysLog = new sysLog();

            try
            {
                result = pf.GetAllBreakDownKPI(sSearch, iDisplayStart, iDisplayLength + iDisplayStart, sSortColumn, sSortDir, out iTotalRecords, new Employee() { ID = UserID }, year);
                iTotalDisplayRecords = iTotalRecords;


                if (result.Count > 0)
                {
                    int idx = iDisplayStart;
                    foreach (KPI p in result)
                    {
                        idx++;
                        IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                        ColumnValues.Add(this.Columns[0], p.ID.ToString());
                        ColumnValues.Add(this.Columns[1], idx.ToString());
                        ColumnValues.Add(this.Columns[2], p.Description.ToString());
                        ColumnValues.Add(this.Columns[3], p.isActive.ToString());
                        ColumnValues.Add(this.Columns[4], p.KPIParent.Description.ToString());
                        ColumnValues.Add(this.Columns[5], p.GroupKPI == null ? string.Empty : p.GroupKPI.Description.ToString());
                        ColumnValues.Add(this.Columns[6], p.CategoryKPI == null ? string.Empty : p.CategoryKPI.Description.ToString());
                        ColumnValues.Add(this.Columns[7], p.TypeKPI == null ? string.Empty : p.TypeKPI.Description.ToString());
                        ColumnValues.Add(this.Columns[8], p.SubTypeKPI == null ? string.Empty : p.SubTypeKPI.Description.ToString());
                        ColumnValues.Add(this.Columns[9], p.Periode.ToString());
                        ColumnValues.Add(this.Columns[10], p.Weight.ToString());
                        ColumnValues.Add(this.Columns[11], p.UoMDetailMax == null ? string.Empty : p.UoMDetailMax.Description.ToString());
                        ColumnValues.Add(this.Columns[12], p.UoMDetailMin == null ? string.Empty : p.UoMDetailMin.Description.ToString());

                        collection.Add(idx, ColumnValues);
                    }
                }

            }
            catch (Exception ex)
            {
                objSysLog = new sysLog();
                objSysLog.Description = ex.Message;
                objSysLog.LogEvent = "Load Data Grid";
                objSysLog.Source = "KPIBreakDownSerializer.ashx";
                objSysLog.CreatedBy = User.Email;
                objSysLog.CreatedDate = DateTime.Now;
                sysLogFacade.Save(objSysLog);
            }
            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}